package com.ittx.dzt.tiktok.service;

import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import open.dto.tiktok.TiktokUncombineDs;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 取消合单
 * <p>
 * cy.tiktok.packages.uncombine
 * /fulfillment/202309/packages/{package_id}/uncombine (取消合单，非欧美地区合单发货使用) https://partner.tiktokshop.com/docv2/page/650aa960f1fd3102b92ceb0e?external_id=650aa960f1fd3102b92ceb0e
 * </p>>
 *
 * @author gywang
 * @since 2024-07-26
 */
@Slf4j
@Service
public class TiktokOrderUncombineService extends AbstractBaseTiktokApiHandler<TiktokUncombineDs> {

    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.ORDER_UNCOMBINE_PACKAGES;
    }

    @Override
    public String getLocalApiMethod() {
        return PlatformApiConstant.InboundInterface.TIKTOK_UNCOMBINE_PACKAGES;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, TiktokUncombineDs ds) {
        Result<?> validResult = checkShopCipherInfo(platformApiParamDTO);
        if (validResult.hasError()) {
            return validResult;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("shop_cipher", getShopCipher(platformApiParamDTO));
        String url = getDefaultUrl(platformApiParamDTO.getStoreConfig());
        url = StringUtils.replace(url, "{package_id}", ds.getSourcePackageId());
        Map<String, Object> reqBody = new HashMap<>();
        reqBody.put("order_ids", ds.getSourceOrderCodes());
        return this.executeTiktokApi(url, platformApiParamDTO.getStoreConfig(), params, JacksonTool.toJson(reqBody), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);

    }
}
